﻿jQuery.extend({
   getScript: function(url, callback) {
      var head = document.getElementsByTagName("head")[0];
      var script = document.createElement("script");
      script.src = url;

      // Handle Script loading
      {
         var done = false;

         // Attach handlers for all browsers
         script.onload = script.onreadystatechange = function(){
            if ( !done && (!this.readyState ||
                  this.readyState == "loaded" || this.readyState == "complete") ) {
               done = true;
               if (callback)
                  callback();

               // Handle memory leak in IE
               script.onload = script.onreadystatechange = null;
            }
         };
      }

      head.appendChild(script);

      // We handle everything using the script element injection
      return undefined;
   }
});

$.extend({
	checkUser: function(viewLimit,sid){
		var result = false;
		switch(viewLimit){
			case 0: // 所有人可看
				result = true;
				$.doCheckResult(viewLimit,result);
				break;
			case 1: // 需要密码
				$.WishWall2.prompt('提示','此许愿墙需要密码才能看，请输入密码：',function(code){
					$.ajax({
						url:ajaxRoot + 'SubjectHandler.ashx',
						dataType:'json',
						type: "get",
						data: {"op":"validate","sid":sid,"viewLimit": viewLimit, "viewCode":code},
						cache:false,
						success:function(msg){
							result = msg.success;
							$.doCheckResult(viewLimit,result);
						}
					});
				});
				
				break;
			default:
				result = false;
		}
	},
	
	doCheckResult:function(viewLimit,passed){
		if(passed){
			$.setLoadPercent(90,'正在加载应用程序...');
			$.loadScripts(['js/app.js','js/add.js','js/list.js'], function(){
				$.setLoadPercent(100,'加载完成');
				//$.WishWall2.InitApp();
                $(document).ready(function() {
			    // 加载完后，才可以正常启动程序  !!!!!!!!!!!
			        //alert('load');
			        $('#body').load(appRoot+'component/body.html',null, $.WishWall2.loadApp);
		        });
			});
		}
		else{
			$('div.validate-tip').show();
		}
	}
});

//$.extend($.WishWall2,{
//    InitApp: function(){
//		$(document).ready(function() {
//			// 加载完后，才可以正常启动程序  !!!!!!!!!!!
//			
//			$('#body').load(appRoot+'component/body.html',null, $.WishWall2.loadApp);
//		});
//	}
//});


$.checkUser(Subject.viewLimit,Subject.id);